package com.example.assignment01_javaee.Controller;


import com.example.assignment01_javaee.Entity.Employee;
import com.example.assignment01_javaee.Service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;
    //获取全部职员信息
    @GetMapping("/employees")
    public ResponseEntity<List<Employee>> getAllEmployees(){
        return ResponseEntity.ok(employeeService.getAllEmployees());
    }
    //通过id获取某个职员信息
    @GetMapping ("/employees/{employeeId}")

    public ResponseEntity<?> getEmployeeById(@PathVariable String employeeId){
        Employee employee = employeeService.getEmployeeById(employeeId);
        if (employee != null) {
            return ResponseEntity.ok(employee);
        } else {
            // 返回错误消息
            String errorMessage = "该职员不存在";
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorMessage);
        }
    }
    //增加职员
    @PostMapping("/employees")
    public String createEmployee(@RequestBody Employee employee){
        return employeeService.createEmployee(employee);
    }

    //根据id更新其他全部信息
    @PutMapping("/employees/{employeeId}")
    public String updateEmployeeById(@RequestBody Employee employee, @PathVariable String employeeId){
        employee.setEmployeeId(employeeId);
        return employeeService.updateEmployeeById(employee);
    }
    //根据id部分更新职位
    @PutMapping("/employees-part/{employeeId}")
    public String updateEmployeePositionById(@RequestParam String position, @PathVariable String employeeId){
        return employeeService.updateEmployeePositionById(position, employeeId);
    }

    @DeleteMapping("/employees/{employeeId}")
    public String deleteEmployeeById(@PathVariable String employeeId){
        return employeeService.deleteEmployeeById(employeeId);
    }

}
